<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-alert
        title=""
        type="success"
        :closable="false">
        <div style="float:left;width:100%;color:#666;">
          <h2>
            <router-link :to="{ path:'add'}">
              <el-button type="success" icon="el-icon-plus">接入自愈</el-button>
            </router-link>
          </h2>针对每一种告警类型配置对应的处理方案（自愈套餐），比如将磁盘使用率告警接入磁盘清理套餐。
        </div>
      </el-alert>
      <div style="margin-top:20px;">
        <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="自愈名称" v-model="QueryItems.Query"></el-input>
        <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
      </div>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
              style="width: 100%">
      <el-table-column align="center" label="序号" width="65">
        <template slot-scope="scope">
          <span>{{scope.row.Id}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="自愈方案标题">
        <template slot-scope="scope">
          <span>{{scope.row.Title}}</span>
        </template>
      </el-table-column>
      <el-table-column width="110px" align="center" label="自愈场景">
        <template slot-scope="scope">
          <span>{{scope.row.Metric}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="运算符">
        <template slot-scope="scope">
          <span>{{scope.row.Operator}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="条件值">
        <template slot-scope="scope">
          <span>{{scope.row.Value}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="添加时间">
        <template slot-scope="scope">
          <span>{{scope.row.InsertTime.substring(0,10)}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="更新时间">
        <template slot-scope="scope">
          <span>{{scope.row.UpdateTime.substring(0,10)}}</span>
        </template>
      </el-table-column>
      <el-table-column   align="center" label="是否开启">
        <template slot-scope="scope">
            <el-button type="success" size="mini" @click="updateStatus(scope.row,scope.row.Isvalid,scope.row.Title)" v-if="scope.row.Isvalid">关闭</el-button>
            <el-button type="info" size="mini" @click="updateStatus(scope.row,scope.row.Isvalid,scope.row.Title)" v-else>开启</el-button>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="添加人">
        <template slot-scope="scope">
          <span>{{scope.row.Author}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="{ path:'edit/'+scope.row.Id}" >
            <el-button type="primary" size="mini" @click="">编辑</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="this.autoStatus" :visible.sync="deldialogFormVisible">
      <template slot-scope="scope">
        <p style="text-align: center;font-size:24px;margin-bottom: 45px;">确认执行当前操作？</p>
        <div slot="footer" class="dialog-footer" style="text-align: center;">
          <el-button @click="cancle()">取 消</el-button>
          <el-button type="primary" @click="handleUpdateStatus()">确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<style>
  .el-dialog {
    width: 30%
  }
</style>
<script>
  import { getList, UpdateAutoStatus } from '@/api/autofill'
  export default {
    data() {
      return {
        tableKey: 0,
        listLoading: true,
        deldialogFormVisible: false,
        listQuery: {
          page: 1,
          limit: 10,
          sort: '-Id',
          query: ''
        },
        QueryItems: {
          Query: ''
        },
        autoStatusForm: {
          Id: '',
          Isvalid: ''
        },
        list: null,
        autoStatus: '',
        total: null
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getList(this.listQuery).then(response => {
          console.log(response.data.list)
          this.list = response.data.list
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      updateStatus(row, status, title) {
        this.autoStatus = status ? '开启' + '>' + title : '关闭' + '>' + title
        this.autoStatusForm = row
        this.autoStatusForm.Isvalid = status ? 0 : 1
        this.deldialogFormVisible = true
      },
      handleUpdateStatus() {
        UpdateAutoStatus(this.autoStatusForm).then(response => {
          if (response.data === 'OK') {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.deldialogFormVisible = false
          } else {
            this.$notify({
              title: '失败',
              message: '更新失败',
              type: 'warning',
              duration: 2000
            })
          }
        }).catch(err => {
          console.log(err)
          this.$notify({
            title: '失败',
            message: '更新失败',
            type: 'warning',
            duration: 2000
          })
        })
        this.deldialogFormVisible = false
      },
      cancle() {
        this.deldialogFormVisible = false
        this.getList()
      },
      handleFilter() {
        this.listQuery.query = JSON.stringify(this.QueryItems)
        this.getList()
      }
    }
  }

</script>